package PenguinsFarm.Renderer
{
	import Interfaces.PenguinsFarm.MICageBackground;
	
	import PenguinsFarm.Penguins.PenguinsRenderer;
	import PenguinsFarm.PenguinsFarmView;
	
	import XFComponent.XFImage;
	
	import XMFramework.Interface.IFramework;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class CageBackground extends Sprite implements MICageBackground
	{
		public static const ON_BACKGROUND_LOADED:String	=	"ON_BACKGROUND_LOADED";
		
		private var imgBackground:XFComponent.XFImage;
		private var penguinsCageMask:BitmapData;
		private var penguinsWaterMask:BitmapData;
		private var isBGLoaded:Boolean	=	false;
		
		private var mpTest:XFComponent.XFImage; 
		
		private var penguinsContainer:Sprite;
		
		public function CageBackground( )
		{
			super();
			
			imgBackground		=	new XFComponent.XFImage();
			addChild( imgBackground );
			
			penguinsContainer	=	new Sprite();
			addChild( penguinsContainer );
			
			Global.CoreSNSFramework.runtimeUpdater.addFunction( onFrameUpdate, null );
		}
		
		private function onBGLoadComplete( bucket:String, clsName:String, clazz:Class ):void
		{
			isBGLoaded	=	true;
			imgBackground.source = new clazz;
			
			var bm:DisplayObject	=	imgBackground.source["Bitmap"];
			
			//=====
			if( !imgBackground.source.hasOwnProperty( "ground" ) )
				throw new Error("Cage Background is not has ground object");
			
			var ground:DisplayObject	=	imgBackground.source["ground"];
			penguinsCageMask	=	new BitmapData( ground.width, ground.height, true, 0x00000000 );
			penguinsCageMask.fillRect( new Rectangle( 0, 0, penguinsCageMask.width, penguinsCageMask.height ), 0x00000000 );
			penguinsCageMask.draw( ground );
			
			//=====
			if( !imgBackground.source.hasOwnProperty( "water" ) )
				throw new Error("Cage Background is not has water object");
			
			var water:DisplayObject	=	imgBackground.source["water"];			
			penguinsWaterMask		=	new BitmapData( water.width, water.height, true, 0x00000000 );
			penguinsWaterMask.fillRect( new Rectangle( 0, 0, penguinsWaterMask.width, penguinsWaterMask.height ), 0x00000000 );
			penguinsWaterMask.draw( water );
			
			//=====
			(Global.CoreSNSFramework.getArchView( ShareMacros.MODULE_PENGUINS_FARM ) as PenguinsFarmView).onResize();
			dispatchEvent( new Event( ON_BACKGROUND_LOADED ) );
		}
		
		private function onFrameUpdate( deltaTime : Number, totalTime : Number ):void
		{
			var childMap:Array	=	new Array();
			var len:int	=	penguinsContainer.numChildren;
			for( var i:int=0; i<len; i++ )
			{
				var p:DisplayObject	=	penguinsContainer.getChildAt( i );
				childMap.push( {"depth":p.y, "ptr":p} );
			}
			
			childMap.sort( depthCompareFunction );
			penguinsContainer.removeChildren();
			for( i=0; i<len; i++ )
				penguinsContainer.addChild( childMap[i].ptr );
		}
		
		private function depthCompareFunction( a:Object, b:Object ):int
		{
			if( a.depth == b.depth )
				return 0;
				
			else if( a.depth > b.depth )
				return 1;
			
			return -1;
		}
		
		public function onResize():void
		{
			imgBackground.x	=	0;
			imgBackground.y	=	0;		
			
			if( imgBackground.source.hasOwnProperty( "ground" ) )
			{
				var ground:DisplayObject	=	imgBackground.source["ground"] as DisplayObject;
				if( ground )
				{
					penguinsContainer.x	=	imgBackground.source.x + ground.x;
					penguinsContainer.y	=	imgBackground.source.y + ground.y;
				}
			}
		}
		
		public function setBackground( bgId:int ):void
		{
			if( penguinsCageMask ) penguinsCageMask.dispose();
			penguinsCageMask	=	null;
			
			if( penguinsWaterMask ) penguinsWaterMask.dispose();
			penguinsWaterMask	=	null;
			
			isBGLoaded	=	false;
			Global.CoreSNSFramework.resourceManager.getResourceFunc( "Background_" + bgId + ".swf", "Background_" + bgId, onBGLoadComplete );
		}
		
		public function isBackgroundLoaded():Boolean
		{
			return isBGLoaded;
		}
		
		public function isHitWater( target:* ):Boolean
		{
			var result:Point	=	new Point( target.x, target.y );
			if( penguinsWaterMask.hitTest( new Point( 0, 0 ), 0x20, result ) )
				return true;
			
			return false;
		}
		
		public function getRandomGroundPoint():Point
		{
			var result:Point	=	new Point( 400, 450 );
		
			while(true)
			{
				result.x	=	Math.random()*penguinsCageMask.width + 0;
				result.y	=	Math.random()*penguinsCageMask.height + 0;
				/*result.x	=	SRandom.randomInt( 0, penguinsCageMask.width );
				result.y	=	SRandom.randomInt( 0, penguinsCageMask.height );*/
				
				if( penguinsCageMask.hitTest( new Point( 0, 0 ), 0x20, result ) )
					break;
			}
			
			return result;
		}		
	
		public function getPenguinsContainer():Sprite
		{
			return penguinsContainer;
		}
		
		
		//================================================================================
		
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			super.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		override public function dispatchEvent(event:Event):Boolean
		{
			return super.dispatchEvent(event);
		}
		
		override public function hasEventListener(type:String):Boolean
		{
			return super.hasEventListener(type);
		}
		
		override public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			super.removeEventListener(type, listener, useCapture);
		}
		
		override public function toString():String
		{
			return super.toString();
		}
		
		override public function willTrigger(type:String):Boolean
		{
			return super.willTrigger(type);
		}
		
	}
}